﻿using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using AnimatorController = UnityEditor.Animations.AnimatorController;
public static class ModelProcessor 
{
    [MenuItem("Assets/Models/Gen Fashion Prefab")]
    static void GenFashionPrefab()
    {
        CreateFashionPrefab(AssetDatabase.GetAssetPath(Selection.activeGameObject));
    }
    public static void CreateFashionPrefab(string fbxPath)
    {
        if (fbxPath.Contains("@"))//动画不处理
            return;
        var obj = AssetDatabase.LoadAssetAtPath(fbxPath, typeof(GameObject)) as GameObject;
        if (null == obj) return;

        var go = GameObject.Instantiate(obj) as GameObject;
        Animator animator = go.GetComponent<Animator>();
        if (null != animator)
        {
            GameObject.DestroyImmediate(animator);
        }
        foreach (var smr in go.GetComponentsInChildren<SkinnedMeshRenderer>())
        {
            smr.quality = SkinQuality.Bone2;
            smr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            smr.receiveShadows = false;
            smr.useLightProbes = false;
            smr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
        }
        int index = fbxPath.LastIndexOf(".");
        var prefabPath = fbxPath.Substring(0, index) + ".prefab";

        AssetDatabase.DeleteAsset(prefabPath);

        PrefabUtility.CreatePrefab(prefabPath,go);

        AssetDatabase.SaveAssets();
        GameObject.DestroyImmediate(go);
    }






    [MenuItem("Assets/Models/Gen Character Prefab")]
    static void GenCharacterPrefab()
    {
        CreateCharacterPrefab(AssetDatabase.GetAssetPath(Selection.activeGameObject),true);
    }
    public static void CreateCharacterPrefab(string fbxPath,bool isOverride)
    {
        if (fbxPath.Contains("@"))//动画不处理
            return;
        var obj = AssetDatabase.LoadAssetAtPath(fbxPath, typeof(GameObject)) as GameObject;
        if (null == obj) return;
        var controllerTemplates = new List<AnimatorController>();
        foreach(var file in System.IO.Directory.GetFiles("Assets/Templates","*.controller"))
        {
            controllerTemplates.Add(AssetDatabase.LoadAssetAtPath<AnimatorController>(file));
        }
        var dir = System.IO.Path.GetDirectoryName(fbxPath);
        var clips = new List<AnimationClip>();
        foreach (var file in System.IO.Directory.GetFiles(dir))
        {
            if (file.EndsWith(".anim")
                || (file.EndsWith(".fbx") && file.Contains("@"))
                )
            {
                clips.Add(AssetDatabase.LoadAssetAtPath<AnimationClip>(file));
            }
        }
        var overrideControllers = new List<AnimatorOverrideController>();
        for (int i = 0; i < controllerTemplates.Count;++i )
        {
            var template = controllerTemplates[i];
            var originalClips = template.animationClips;

            var overrideController = new AnimatorOverrideController();
            overrideController.runtimeAnimatorController = controllerTemplates[i];
            
            overrideControllers.Add(overrideController);
            for (int j = 0; j < originalClips.Length; ++j)
            {
                var originalClip = originalClips[j];
                AnimationClip destClip = null;

                for (int k = 0; k < clips.Count; ++k)
                {
                    if (clips[k].name.Equals(destClip.name))
                    {
                        destClip = clips[k];
                        break;
                    }
                }
                overrideController[originalClip.name] = destClip;
            }
            
            AssetDatabase.CreateAsset(overrideController, dir + "/" + template.name +".controller");
        }
        var go = GameObject.Instantiate(obj) as GameObject;
        var animator = go.GetComponent<Animator>();
        if(null == animator)
            animator = go.AddComponent<Animator>();
        animator.runtimeAnimatorController = overrideControllers[0];
        animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
        foreach (var smr in go.GetComponentsInChildren<SkinnedMeshRenderer>())
        {
            smr.quality = SkinQuality.Bone2;
            smr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            smr.receiveShadows = false;
            smr.useLightProbes = false;
            smr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
        }
        var index = fbxPath.LastIndexOf(".");
        PrefabUtility.CreatePrefab(fbxPath.Substring(0,index) +".prefab",go);

        AssetDatabase.SaveAssets();

        GameObject.DestroyImmediate(go);
    }
}